package com.damai.mvc.view.imple;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import com.citywithincity.utils.IoUtil;
import com.damai.mvc.view.View;

public class FileView implements View {

	private File file;
	
	public FileView(File file){
		this.file = file;
	}
	
	@Override
	public void renderTo(HttpServletResponse response) throws IOException {
		response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "utf-8"));
		FileInputStream inputStream = new FileInputStream(file);
		String length = String.valueOf( inputStream.available());
		response.setHeader("Content-Length",length  );
		IoUtil.copyAndCloseAll(inputStream, response.getOutputStream());
	}

	

}
